/**
 * 
 */
package com.xb.cloud.product.merchant.inner.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.product.merchant.inner.service.ProductPicInnerService;
import com.xb.cloud.product.merchant.inner.service.ProductSerialInnerService;
import com.xb.cloud.product.merchant.service.client.bo.ProductPicBo;
import com.xb.cloud.product.merchant.service.impl.AbstractService;
import com.xb.cloud.product.merchant.service.impl.ProductPublishServiceImpl;
import com.xb.cloud.product.service.business.model.ProductPic;
import com.xb.cloud.product.service.business.model.ProductPicExample;
import com.xb.cloud.product.service.business.model.ProductPicExample.Criteria;
import com.xb.cloud.product.service.business.model.mapper.ProductPicMapper;

/**
 * 商品图片服务类
 * 
 * @author kexue
 *
 */
@Service
public class ProductPicInnerServiceImpl extends AbstractService implements ProductPicInnerService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ProductPicInnerServiceImpl.class);

	@Resource
	private ProductPicMapper productPicMapper;
	
	@Resource
	private ProductSerialInnerService  productSerialInnerService;

	/**
	 * 取得商品的图片列表
	 * 
	 * @param productId
	 * @return
	 */
	@Override
	public List<ProductPicBo> getPicListByProductId(Long productId) {

		ProductPicExample productPicExample = new ProductPicExample();
		Criteria createCriteria = productPicExample.createCriteria();
		createCriteria.andProductIdEqualTo(productId);
		createCriteria.andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE);
		createCriteria.andIsEnabledEqualTo(DbConstants.TRUE);
		List<ProductPic> productPicList = productPicMapper.selectByExample(productPicExample);
		if(productPicList!=null && productPicList.size()>0){
			List<ProductPicBo> productPicBoList = BeanUtils.newListAndCopyProperties(ProductPicBo.class,
					productPicList);
			return productPicBoList;
		}
		 
		return null;
	}

	/**
	 * 取得默认商品的图片
	 * 
	 * @param productId
	 * @return
	 */
	@Override
	public ProductPicBo getDefaultPicListByProductId(Long productId) {

		ProductPicExample productPicExample = new ProductPicExample();
		Criteria createCriteria = productPicExample.createCriteria();
		createCriteria.andProductIdEqualTo(productId);
		createCriteria.andIsDefaultEqualTo(DbConstants.TRUE);
		createCriteria.andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE);
		createCriteria.andIsEnabledEqualTo(DbConstants.TRUE);
		List<ProductPic> productPicList = productPicMapper.selectByExample(productPicExample);

		ProductPic productPic = (productPicList == null || productPicList.size() == 0) ? null : productPicList.get(0);
		return productPic == null ? null : BeanUtils.newAndCopyProperties(ProductPicBo.class, productPic);
	}
	
	
	/**
	 * 取得虚品对应的主品的默认图片
	 * @param productId
	 * @return
	 */
	public ProductPicBo  getDefaultPicByrVirtualProductId(Long virtualProductId){
		 Long defaultProductId = productSerialInnerService.getDefaultProductIdByVirtualId(virtualProductId);
		 if(defaultProductId!=null){
			 return getDefaultPicListByProductId(defaultProductId);
		 }else{
			 return null;
		 }
	}
}
